package cn.kmsoft.stgeometry.util;

import cn.kmsoft.stgeometry.landm.FieldInfo;
import cn.kmsoft.stgeometry.landm.Land;
import org.json.JSONException;
import org.opengis.referencing.FactoryException;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Txt转换为shp：
 * 1、支持批量转换，相同度带的txt输出到一个shp文件中，在bz字段中以txt文件名称区分
 * 2、可以跨带转换，数据分带号DegreeNum以坐标值的前2两位（35，36，37）为依据进行自动划分
 * 3、输出结果中，最多有3个shp文件(35.shp,36.shp,37.shp)；
 * 4、包含了处理成功的文件和失败的文件结果
 * <p>
 * <p>
 * 1,正在将txt转换为Land对象，放在
 * 2,正在对Land对象不同度带(35,36,37)分析拆分
 * 3,循环将Land对象插入到SHP文件中
 * 4,按照投影的度带号记录转换成功和失败的详细信息
 *
 * @author wangq
 */
public class Txt2Shp4 {

    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS");
        String beginTime = sdf.format(new Date());

        System.out.println();
        System.out.println("---开始处理---," + beginTime);
        /**********************开始处理*************************/

        txt2shp4_bat(YwType.tdcb);

        /**********************结束处理*************************/
        String endTime = sdf.format(new Date());
        System.out.println();
        System.out.println("---结束处理---," + endTime);
    }

    public static void txt2shp4_bat(YwType ywtype) throws JSONException, IOException, FactoryException {
        //String encoding = "GBK";
        String encoding = "utf-8";
        //1-设置路径
        String txtfolder = "D:\\temp\\dhtool\\漆璐\\永川区储备矢量数据（区土储中心提交）\\新城";
        String outfilepath = "D:\\temp\\dhtool\\漆璐\\永川区储备矢量数据（区土储中心提交）";
        String qxmc = "yongchuan";
        //tip: 划拨、出让、未知、补充、其他
        String tdgyfs = "sanjiao";

        //2-坐标系与投影定义
        //EPSG:4496  CGCS2000 / 3-degree Gauss-Kruger zone 18
        String wkt18 = "PROJCS[\"CGCS2000_Gauss_Kruger_zone_18\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",105],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",18500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //EPSG:4523  CGCS2000 / 3-degree Gauss-Kruger zone 35
        String wkt35 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_35\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",105],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",35500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //EPSG:4524  CGCS2000 / 3-degree Gauss-Kruger zone 36
        String wkt36 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_36\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",108],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",36500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //EPSG:4525  CGCS2000 / 3-degree Gauss-Kruger zone 37
        String wkt37 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_37\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",111],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",37500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //重庆主城独立坐标
        String wktcqdl = "PROJCS[\"CQDULI\",GEOGCS[\"GCS_Beijing_1954\",DATUM[\"D_Beijing_1954\",SPHEROID[\"Krasovsky_1940\",6378245.0,298.3]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",50000.0],PARAMETER[\"False_Northing\",-3202855.272],PARAMETER[\"Central_Meridian\",106.419263889],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";

        //3-加载所有txt
        File inputDir = new File(txtfolder);
        String txtfilePath = inputDir.getAbsolutePath();
        Map<String, Land> allLands = new HashMap<String, Land>();

        System.out.println();
        System.out.println("---------------第1步,正在将txt转换为Land对象中------------------");
        int iNum = 0;
        File[] fs = inputDir.listFiles();
        if (fs != null) {
            for (File f : fs) {
                if (f.isFile() && f.getAbsolutePath().toLowerCase().endsWith(".txt")) {
                    //if(f.isFile() && f.getAbsolutePath().toLowerCase().endsWith(".cg") ){
                    iNum++;

                    try {
                        System.out.println("第 " + iNum + " 个txt转换Land对象-------------------------");
                        //执行单个转换
                        //txt路径
                        String txtfileName = f.getName();
                        String txtfile = f.getAbsolutePath();

                        int endIndex = txtfileName.lastIndexOf(".");
                        String fileNameNoExt = txtfileName.substring(0, endIndex);

                        System.out.println("读取txt文本：" + txtfileName);
                        Land land = TxtUtil.readTxtFile2(txtfile, encoding);

                        //按照txt文件名为key进行存储
                        allLands.put(fileNameNoExt, land);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        //
                    }
                }
            }
        }

        //4-拆分到3个分度带
        System.out.println();
        System.out.println("---------------第2步,正在对Land对象不同度带(35,36,37)分析拆分中------------------");
        Map<String, Map<String, Land>> allDegreeLands = new HashMap<String, Map<String, Land>>();

        Map<String, Land> degree18Lands = new HashMap<String, Land>();
        Map<String, Land> degree35Lands = new HashMap<String, Land>();
        Map<String, Land> degree36Lands = new HashMap<String, Land>();
        Map<String, Land> degree37Lands = new HashMap<String, Land>();
        Map<String, Land> degreeCqdlLands = new HashMap<String, Land>();

        Set<String> dzjghSet = allLands.keySet();
        Iterator<String> dzjghIter = dzjghSet.iterator();
        while (dzjghIter.hasNext()) {
            String dzjgh = dzjghIter.next();
            Land land = allLands.get(dzjgh);
            if ("18".equalsIgnoreCase(land.getDegreeNum())) {
                degree18Lands.put(dzjgh, land);
            } else if ("35".equalsIgnoreCase(land.getDegreeNum())) {
                degree35Lands.put(dzjgh, land);
            } else if ("36".equalsIgnoreCase(land.getDegreeNum())) {
                degree36Lands.put(dzjgh, land);
            } else if ("37".equalsIgnoreCase(land.getDegreeNum())) {
                degree37Lands.put(dzjgh, land);
            } else if ("cqdl".equalsIgnoreCase(land.getDegreeNum())) {
                degreeCqdlLands.put(dzjgh, land);
            }
        }

        if (degree18Lands.size() > 0) {
            allDegreeLands.put("18", degree18Lands);
        }
        if (degree35Lands.size() > 0) {
            allDegreeLands.put("35", degree35Lands);
        }
        if (degree36Lands.size() > 0) {
            allDegreeLands.put("36", degree36Lands);
        }
        if (degree37Lands.size() > 0) {
            allDegreeLands.put("37", degree37Lands);
        }
        if (degreeCqdlLands.size() > 0) {
            allDegreeLands.put("cqdl", degreeCqdlLands);
        }

        //5-定义字段内容
        String geometryType = "esriGeometryPolygon";
        String fields = "";
        if (ywtype == YwType.tdzs) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"jsscbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"jsscbh\"},"
                    + "{\"name\":\"xmmc\",\"type\":\"esriFieldTypeString\",\"alias\":\"xmmc\"},"
                    + "{\"name\":\"pzwh\",\"type\":\"esriFieldTypeString\",\"alias\":\"pzwh\"},"
                    + "{\"name\":\"htbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"htbh\"},"
                    + "{\"name\":\"dkmj\",\"type\":\"esriFieldTypeDouble\",\"alias\":\"dkmj\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        } else if (ywtype == YwType.tdgy) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"jsscbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"jsscbh\"},"
                    + "{\"name\":\"xmmc\",\"type\":\"esriFieldTypeString\",\"alias\":\"xmmc\"},"
                    + "{\"name\":\"dzjgh\",\"type\":\"esriFieldTypeString\",\"alias\":\"dzjgh\"},"
                    + "{\"name\":\"htbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"htbh\"},"
                    + "{\"name\":\"dkmj\",\"type\":\"esriFieldTypeDouble\",\"alias\":\"dkmj\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        } else if (ywtype == YwType.tdgyql) {
            fields = "["
                    + "{\"name\":\"DZJGH\",\"type\":\"esriFieldTypeString\",\"alias\":\"DZJGH\"}"
                    + "]";
        } else if (ywtype == YwType.tdcb) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"jsscbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"jsscbh\"},"
                    + "{\"name\":\"xmmc\",\"type\":\"esriFieldTypeString\",\"alias\":\"xmmc\"},"
                    + "{\"name\":\"cbpwh\",\"type\":\"esriFieldTypeString\",\"alias\":\"cbpwh\"},"
                    + "{\"name\":\"htbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"htbh\"},"
                    + "{\"name\":\"dkmj\",\"type\":\"esriFieldTypeDouble\",\"alias\":\"dkmj\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        } else if (ywtype == YwType.other) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"name\",\"type\":\"esriFieldTypeString\",\"alias\":\"name\"},"
                    + "{\"name\":\"type\",\"type\":\"esriFieldTypeString\",\"alias\":\"type\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        }


        //List<FieldInfo> fieldInfos = makeFields();
        //String fields2 = org.json.simple.JSONObject.
        Map<String, Map<String, Set<String>>> alldegreeResults = new HashMap<String, Map<String, Set<String>>>();

        //6-land转shp
        System.out.println();
        System.out.println("---------------第3步,按不同度带将Land对象生成SHP图形------------------");
        //分度带18、35、36、37、cqdl进行转换
        Set<String> degreeSet = allDegreeLands.keySet();
        Iterator<String> degreeIter = degreeSet.iterator();
        while (degreeIter.hasNext()) {
            String degree = degreeIter.next();
            Map<String, Land> degreeLand = allDegreeLands.get(degree);

            // 创建shape文件对象
            File outshpfile = null;
            String wkt = "";
            if ("18".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp18-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt18;
            } else if ("35".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp35-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt35;
            } else if ("36".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp36-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt36;
            } else if ("37".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp37-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt37;
            } else if ("cqdl".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shpcqdl-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wktcqdl;
            } else {
                continue;
            }

            //SHP转换
            //System.out.println();
            System.out.println(String.format("正在处理第 %s 度带的SHP转换-->输出文件地址:%s ", degree, outshpfile.getAbsolutePath()));
            Map<String, Set<String>> res = GeoJson2Shapefile.createShapefile3(
                    outshpfile,
                    degreeLand,
                    wkt,
                    geometryType.toLowerCase(),
                    fields);

            alldegreeResults.put(degree, res);
        }

        //7-结果统计
        System.out.println();
        System.out.println();
        System.out.println("---------------第4步, 数据处理结果统计------------------");

        //投影的度带列表
        Set<String> degreeKeySet = alldegreeResults.keySet();
        int numOfDegree = degreeKeySet.size();
        System.out.println(String.format("--->一共包含：%d 个度带!  ", numOfDegree));

        Iterator<String> degreeKeyIter = degreeKeySet.iterator();
        while (degreeKeyIter.hasNext()) {
            //度带号
            String degree = degreeKeyIter.next();
            Map<String, Set<String>> res = alldegreeResults.get(degree);
            Set<String> successSet = res.get("success");
            Set<String> failSet = res.get("fail");

            System.out.println();
            System.out.println(String.format("--->度带：%s, 成功：%d 个数据，失败：%d 条数据  ", degree, successSet.size(), failSet.size()));

            //1-成功部分
            System.out.println(String.format("成功的DZJGH列表为："));
            Iterator<String> successIter = successSet.iterator();
            while (successIter.hasNext()) {
                String dzjgh = successIter.next();
                System.out.println(String.format(" %s ", dzjgh));
            }

            //2-失败部分
            System.out.println(String.format("失败的DZJGH列表为： "));
            Iterator<String> failIter = failSet.iterator();
            while (failIter.hasNext()) {
                String dzjgh = failIter.next();
                System.out.println(String.format(" %s ", dzjgh));
            }
        }
        System.out.println();
    }

    public static void txt2shp4_bat2(YwType ywtype) throws JSONException, IOException, FactoryException {
        //String encoding = "GBK";
        String encoding = "utf-8";
        //1-设置路径
        String txtfolder = "D:\\temp\\dhtool\\wkt2shp\\rkdk";
        String outfilepath = "D:\\temp\\dhtool\\wkt2shp";
        String qxmc = "cqs";
        //tip: 划拨、出让、未知、补充、其他
        String tdgyfs = "tdcb";

        //2-坐标系与投影定义
        //EPSG:4496  CGCS2000 / 3-degree Gauss-Kruger zone 18
        String wkt18 = "PROJCS[\"CGCS2000_Gauss_Kruger_zone_18\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",105],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",18500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //EPSG:4523  CGCS2000 / 3-degree Gauss-Kruger zone 35
        String wkt35 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_35\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",105],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",35500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //EPSG:4524  CGCS2000 / 3-degree Gauss-Kruger zone 36
        String wkt36 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_36\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",108],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",36500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //EPSG:4525  CGCS2000 / 3-degree Gauss-Kruger zone 37
        String wkt37 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_37\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",111],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",37500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]";
        //重庆主城独立坐标
        String wktcqdl = "PROJCS[\"CQDULI\",GEOGCS[\"GCS_Beijing_1954\",DATUM[\"D_Beijing_1954\",SPHEROID[\"Krasovsky_1940\",6378245.0,298.3]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",50000.0],PARAMETER[\"False_Northing\",-3202855.272],PARAMETER[\"Central_Meridian\",106.419263889],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";

        //3-加载所有txt
        File inputDir = new File(txtfolder);
        String txtfilePath = inputDir.getAbsolutePath();
        Map<String, Land> allLands = new HashMap<String, Land>();

        System.out.println();
        System.out.println("---------------第1步,正在将txt转换为Land对象中------------------");
        int iNum = 0;
        File[] fs = inputDir.listFiles();
        if (fs != null) {
            for (File f : fs) {
                if (f.isFile() && f.getAbsolutePath().toLowerCase().endsWith(".txt")) {
                    //if(f.isFile() && f.getAbsolutePath().toLowerCase().endsWith(".cg") ){
                    iNum++;

                    try {
                        System.out.println("第 " + iNum + " 个txt转换Land对象-------------------------");
                        //执行单个转换
                        //txt路径
                        String txtfileName = f.getName();
                        String txtfile = f.getAbsolutePath();

                        int endIndex = txtfileName.lastIndexOf(".");
                        String fileNameNoExt = txtfileName.substring(0, endIndex);

                        //注意，这里不一样。这里至解析txt的坐标串信息。避免了txt中文乱码数据的问题
                        System.out.println("读取txt文本：" + txtfileName);
                        Land land = TxtUtil.readTxtFile1(txtfile, encoding);

                        //按照txt文件名为key进行存储
                        allLands.put(fileNameNoExt, land);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        //
                    }
                }
            }
        }

        //4-拆分到3个分度带
        System.out.println();
        System.out.println("---------------第2步,正在对Land对象不同度带(35,36,37)分析拆分中------------------");
        Map<String, Map<String, Land>> allDegreeLands = new HashMap<String, Map<String, Land>>();

        Map<String, Land> degree18Lands = new HashMap<String, Land>();
        Map<String, Land> degree35Lands = new HashMap<String, Land>();
        Map<String, Land> degree36Lands = new HashMap<String, Land>();
        Map<String, Land> degree37Lands = new HashMap<String, Land>();
        Map<String, Land> degreeCqdlLands = new HashMap<String, Land>();

        Set<String> dzjghSet = allLands.keySet();
        Iterator<String> dzjghIter = dzjghSet.iterator();
        while (dzjghIter.hasNext()) {
            String dzjgh = dzjghIter.next();
            Land land = allLands.get(dzjgh);
            if ("18".equalsIgnoreCase(land.getDegreeNum())) {
                degree18Lands.put(dzjgh, land);
            } else if ("35".equalsIgnoreCase(land.getDegreeNum())) {
                degree35Lands.put(dzjgh, land);
            } else if ("36".equalsIgnoreCase(land.getDegreeNum())) {
                degree36Lands.put(dzjgh, land);
            } else if ("37".equalsIgnoreCase(land.getDegreeNum())) {
                degree37Lands.put(dzjgh, land);
            } else if ("cqdl".equalsIgnoreCase(land.getDegreeNum())) {
                degreeCqdlLands.put(dzjgh, land);
            }
        }

        if (degree18Lands.size() > 0) {
            allDegreeLands.put("18", degree18Lands);
        }
        if (degree35Lands.size() > 0) {
            allDegreeLands.put("35", degree35Lands);
        }
        if (degree36Lands.size() > 0) {
            allDegreeLands.put("36", degree36Lands);
        }
        if (degree37Lands.size() > 0) {
            allDegreeLands.put("37", degree37Lands);
        }
        if (degreeCqdlLands.size() > 0) {
            allDegreeLands.put("cqdl", degreeCqdlLands);
        }

        //5-定义字段内容
        String geometryType = "esriGeometryPolygon";
        String fields = "";
        if (ywtype == YwType.tdzs) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"jsscbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"jsscbh\"},"
                    + "{\"name\":\"xmmc\",\"type\":\"esriFieldTypeString\",\"alias\":\"xmmc\"},"
                    + "{\"name\":\"pzwh\",\"type\":\"esriFieldTypeString\",\"alias\":\"pzwh\"},"
                    + "{\"name\":\"htbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"htbh\"},"
                    + "{\"name\":\"dkmj\",\"type\":\"esriFieldTypeDouble\",\"alias\":\"dkmj\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        } else if (ywtype == YwType.tdgy) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"jsscbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"jsscbh\"},"
                    + "{\"name\":\"xmmc\",\"type\":\"esriFieldTypeString\",\"alias\":\"xmmc\"},"
                    + "{\"name\":\"dzjgh\",\"type\":\"esriFieldTypeString\",\"alias\":\"dzjgh\"},"
                    + "{\"name\":\"htbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"htbh\"},"
                    + "{\"name\":\"dkmj\",\"type\":\"esriFieldTypeDouble\",\"alias\":\"dkmj\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        } else if (ywtype == YwType.tdgyql) {
            fields = "["
                    + "{\"name\":\"DZJGH\",\"type\":\"esriFieldTypeString\",\"alias\":\"DZJGH\"}"
                    + "]";
        } else if (ywtype == YwType.tdcb) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"jsscbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"jsscbh\"},"
                    + "{\"name\":\"xmmc\",\"type\":\"esriFieldTypeString\",\"alias\":\"xmmc\"},"
                    + "{\"name\":\"cbpwh\",\"type\":\"esriFieldTypeString\",\"alias\":\"cbpwh\"},"
                    + "{\"name\":\"htbh\",\"type\":\"esriFieldTypeString\",\"alias\":\"htbh\"},"
                    + "{\"name\":\"dkmj\",\"type\":\"esriFieldTypeDouble\",\"alias\":\"dkmj\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        } else if (ywtype == YwType.other) {
            fields = "["
                    + "{\"name\":\"qxdm\",\"type\":\"esriFieldTypeString\",\"alias\":\"qxdm\"},"
                    + "{\"name\":\"name\",\"type\":\"esriFieldTypeString\",\"alias\":\"name\"},"
                    + "{\"name\":\"type\",\"type\":\"esriFieldTypeString\",\"alias\":\"type\"},"
                    + "{\"name\":\"bz\",\"type\":\"esriFieldTypeString\",\"alias\":\"bz\"}"
                    + "]";
        }


        //List<FieldInfo> fieldInfos = makeFields();
        //String fields2 = org.json.simple.JSONObject.
        Map<String, Map<String, Set<String>>> alldegreeResults = new HashMap<String, Map<String, Set<String>>>();

        //6-land转shp
        System.out.println();
        System.out.println("---------------第3步,按不同度带将Land对象生成SHP图形------------------");
        //分度带18、35、36、37、cqdl进行转换
        Set<String> degreeSet = allDegreeLands.keySet();
        Iterator<String> degreeIter = degreeSet.iterator();
        while (degreeIter.hasNext()) {
            String degree = degreeIter.next();
            Map<String, Land> degreeLand = allDegreeLands.get(degree);

            // 创建shape文件对象
            File outshpfile = null;
            String wkt = "";
            if ("18".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp18-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt18;
            } else if ("35".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp35-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt35;
            } else if ("36".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp36-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt36;
            } else if ("37".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shp37-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wkt37;
            } else if ("cqdl".equalsIgnoreCase(degree)) {
                outshpfile = new File(String.format("%s\\%s-shpcqdl-%s.shp", outfilepath, qxmc, tdgyfs));
                wkt = wktcqdl;
            } else {
                continue;
            }

            //SHP转换
            //System.out.println();
            System.out.println(String.format("正在处理第 %s 度带的SHP转换-->输出文件地址:%s ", degree, outshpfile.getAbsolutePath()));
            Map<String, Set<String>> res = GeoJson2Shapefile.createShapefile3(
                    outshpfile,
                    degreeLand,
                    wkt,
                    geometryType.toLowerCase(),
                    fields);

            alldegreeResults.put(degree, res);
        }

        //7-结果统计
        System.out.println();
        System.out.println();
        System.out.println("---------------第4步, 数据处理结果统计------------------");

        //投影的度带列表
        Set<String> degreeKeySet = alldegreeResults.keySet();
        int numOfDegree = degreeKeySet.size();
        System.out.println(String.format("--->一共包含：%d 个度带!  ", numOfDegree));

        Iterator<String> degreeKeyIter = degreeKeySet.iterator();
        while (degreeKeyIter.hasNext()) {
            //度带号
            String degree = degreeKeyIter.next();
            Map<String, Set<String>> res = alldegreeResults.get(degree);
            Set<String> successSet = res.get("success");
            Set<String> failSet = res.get("fail");

            System.out.println();
            System.out.println(String.format("--->度带：%s, 成功：%d 个数据，失败：%d 条数据  ", degree, successSet.size(), failSet.size()));

            //1-成功部分
            System.out.println(String.format("成功的DZJGH列表为："));
            Iterator<String> successIter = successSet.iterator();
            while (successIter.hasNext()) {
                String dzjgh = successIter.next();
                System.out.println(String.format(" %s ", dzjgh));
            }

            //2-失败部分
            System.out.println(String.format("失败的DZJGH列表为： "));
            Iterator<String> failIter = failSet.iterator();
            while (failIter.hasNext()) {
                String dzjgh = failIter.next();
                System.out.println(String.format(" %s ", dzjgh));
            }
        }
        System.out.println();
    }

    public static List<FieldInfo> makeFields() {
        List<FieldInfo> fieldInfos = new ArrayList<FieldInfo>();

        FieldInfo fieldInfo1 = new FieldInfo();
        fieldInfo1.setName("name");
        fieldInfo1.setAlias("name");
        fieldInfo1.setType("esriFieldTypeString");
        fieldInfos.add(fieldInfo1);

        FieldInfo fieldInfo2 = new FieldInfo();
        fieldInfo2.setName("type");
        fieldInfo2.setAlias("type");
        fieldInfo2.setType("esriFieldTypeString");
        fieldInfos.add(fieldInfo2);

        FieldInfo fieldInfo3 = new FieldInfo();
        fieldInfo3.setName("bz");
        fieldInfo3.setAlias("bz");
        fieldInfo3.setType("esriFieldTypeString");
        fieldInfos.add(fieldInfo3);

        FieldInfo fieldInfo4 = new FieldInfo();
        fieldInfo4.setName("dzjgh");
        fieldInfo4.setAlias("dzjgh");
        fieldInfo4.setType("esriFieldTypeString");
        fieldInfos.add(fieldInfo4);

        FieldInfo fieldInfo5 = new FieldInfo();
        fieldInfo5.setName("htbh");
        fieldInfo5.setAlias("htbh");
        fieldInfo5.setType("esriFieldTypeString");
        fieldInfos.add(fieldInfo5);

        return fieldInfos;
    }
}
